#include <iostream>
#include "platinum.h"

bool quit = false;
void HandleInput(void* Conn)
{
     std::string data;
     Platinum::Net::TCP::Connection* Remote = reinterpret_cast<Platinum::Net::TCP::Connection*>(Remote);

     while(!quit)
     {
          std::getline(std::cin, data, '\n');
          Platinum::Net::TCP::Send(Remote,data);
          if(data == "/quit")
               quit = true;
     }
}

void ProcessError(Platinum::Error e)
{
     std::cout << e.errCode << ": " << e.errMessage << std::endl;
     quit = true;
}

int main(int argc, char* argv[])
{
     if(argc > 2)
     {
          std::string msg,IP = argv[1];
          std::stringstream tmp;
          int iPort;

          tmp << argv[2];
          tmp >> iPort;
          
          using namespace Platinum;
          Net::TCP::Connection* Remote = new Net::TCP::Connection();
          try{

               Net::TCP::Initialize(Remote);
               Net::TCP::Connect(Remote,IP,iPort);
               Thread::Create(HandleInput,(void*)Remote);

          }catch(Error(e)){
               ProcessError(e);
          }

          while(!quit)
          {
               msg = Net::TCP::Recv(Remote);
               std::cout << msg;
               Thread::Yield();              
          }                   

     }
}
